<template>
  <el-row type="flex" align="middle" justify="space-between" style="height: 40px; width:100%">
    <el-col>
      <!-- 左侧内容 -->
      <span>{{ treeNode.name }}</span>
    </el-col>
    <el-col>
      <!-- 右侧内容 -->
      <el-row type="flex" justify="end">
        <el-col :span="5">{{ treeNode.manager }}</el-col>
        <el-col :span="3">
          <el-dropdown @command="oprateDepts">
            <span class="el-dropdown-link">
              操作<i class="el-icon-arrow-down el-icon--right" />
            </span>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item command="add">添加子部门</el-dropdown-item>
              <el-dropdown-item v-if="!isRoot" command="edit">编辑部门</el-dropdown-item>
              <el-dropdown-item v-if="!isRoot" command="del">删除部门</el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </el-col>
      </el-row>
    </el-col>
  </el-row>
</template>
<script>
import { delDepartments, addDepartments } from '@/api/departments'

export default {
  name: 'TreeTools',
  props: {
    treeNode: {
      required: true,
      type: Object
    },
    isRoot: {
      default: false,
      type: Boolean
    }
  },
  methods: {
    /**  根据按钮操作部门  */
    oprateDepts(type) {
      if (type === 'add') {
        // 新增
        // 打开添加子部门窗口弹层，传出当前点击的部门（this.treeNode）
        this.$emit('openAddDeptWindow', this.treeNode)
        addDepartments({})
      } else if (type === 'edit') {
        // 编辑
        this.$emit('openEditDeptWindow', this.treeNode)
      } else {
        // 删除
        // 弹窗，用户点击确定将执行then的操作，此时返回一个promise对象，请求成功后，将再次执行then函数
        this.$confirm('确定要执行删除操作吗？此操作无法恢复。').then(() => {
          return delDepartments(this.treeNode.id)
        }).then(() => {
          //  后端数据已经改变，前端数据还未改变，需要重新获取数据
          this.$emit('delDepts')
        })
      }
    }
  }
}
</script>
<style scoped></style>
